﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Globalization;

namespace SwenAssigment.Classes
{
    public class GenDate
    {
        public static List<DateTime> AllDateInYear(int year)
        {
            List<DateTime> AllDate = new List<DateTime>();
            for (int i = 1; i != 13; i++)
            {
                foreach (DateTime s in AllDatesInAMonth(i, year))
                {
                    AllDate.Add(s);
                }
            }
            return AllDate;
        }
        public static List<DateTime> AllDatesInAMonth(int month, int year)
        {
            var firstOftargetMonth = new DateTime(year, month, 1);
            var firstOfNextMonth = firstOftargetMonth.AddMonths(1);
            var allDates = new List<DateTime>();
            for (DateTime date = firstOftargetMonth; date < firstOfNextMonth; date = date.AddDays(1))
            {
                allDates.Add(date);
            }
            return allDates;
        }
        public static DateTime FirstDateOfWeek(int year, int weekOfYear)
        {
            DateTime jan1 = new DateTime(year, 1, 1);
            int daysOffset = DayOfWeek.Thursday - jan1.DayOfWeek;

            DateTime firstThursday = jan1.AddDays(daysOffset);
            var cal = CultureInfo.CurrentCulture.Calendar;
            int firstWeek = cal.GetWeekOfYear(firstThursday, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday);

            var weekNum = weekOfYear;
            if (firstWeek <= 1)
            {
                weekNum -= 1;
            }
            var result = firstThursday.AddDays(weekNum * 7);
            return result.AddDays(-3);
        }
        public static List<WeekDate> AllWeekAndDate(int years)
        {
            List<WeekDate> AllWeek = new List<WeekDate>();
            CultureInfo myCI = new CultureInfo("zh-SG");
            System.Globalization.Calendar myCal = myCI.Calendar;

            // Gets the DTFI properties required by GetWeekOfYear.
            CalendarWeekRule myCWR = myCI.DateTimeFormat.CalendarWeekRule;
            DayOfWeek myFirstDOW = myCI.DateTimeFormat.FirstDayOfWeek;
            DateTime LastDay = new System.DateTime(years, 12, 31);
            int TotalWeek = myCal.GetWeekOfYear(LastDay, myCWR, myFirstDOW);
            //int weekCount = myCal.GetWeekOfYear(LastDay, myCWR, myFirstDOW);
            for (int i = 1; i <= TotalWeek; i++)
            {
                WeekDate newWeek = new WeekDate();
                newWeek.WeekNo = i;
                List<DateTime> WeekDate = new List<DateTime>();
                for (int k = 0; k != 7; k++)
                {
                    WeekDate.Add(FirstDateOfWeek(years, myCal.GetWeekOfYear(LastDay, myCWR, myFirstDOW) - (TotalWeek - i)).AddDays(k));
                }
                newWeek.date = WeekDate;
                AllWeek.Add(newWeek);
            }
            return AllWeek;
        }

    }
}